﻿using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;

namespace SeaDragonViewer.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public partial class Viewer : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling using
        // the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public Viewer()
        {
        }

        string xmlPath;
        string _width, _height;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true)]
        public string XMLPath
        {
            get { 
                // Nik20130123 - If the DZC querystring parameter is present, then override the xml specified in the web part's properties by its value;
                try
                {
                    if (Page.Request.QueryString["DZC"] != null)
                        return Page.Request.QueryString["DZC"].ToString();
                    else
                        return this.xmlPath;
                }
                catch { return ""; }
            }
            set { this.xmlPath = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true)]
        public string WPWidth
        {
            get { return this._width; }
            set { this._width = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true)]
        public string WPHeight
        {
            get { return this._height; }
            set { this._height = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<div id=\"container\" style=\"width:" + _width +"; height:" + _height + ";\"></div>");
            sb.Append("<script src=\"/Style Library/js/SeaDragonJS/seadragon-dev.js\"></script>");
            sb.Append("<script src=\"/Style Library/js/SeaDragonJS/src/seadragon.TouchController.js\"></script>");
            sb.Append("<script>");
            sb.Append("var viewer = new Seadragon.Viewer(\"container\");");
            sb.Append("viewer.addEventListener(\"open\", onViewerOpen);");
            sb.Append("viewer.openDzi(\"" + XMLPath + "\");");
            sb.Append("function onViewerOpen(event){");
            sb.Append("var touchController = new Seadragon.TouchController(viewer);");
            sb.Append("}");
            sb.Append("</script>");

            litSeaDragon.Text = sb.ToString();
        }
    }
}
